﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Data.OleDb;
using LumenWorks.Framework.IO.Csv;


namespace estiglp1112_5946_csharp
{
    public partial class Form1 : Form
    {
        List<Teacher> teachers ;

        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Form principal do programa
        /// </summary>
        

        /// <summary>
        /// Chama um dialog para selecção do ficheiro csv
        /// </summary>
        /// <returns></returns>
        private string fileDialog()
        {
            string filename = "";
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = ".";
            openFileDialog1.Filter = "CSV files (*.CSV)|*.csv";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                filename = openFileDialog1.FileName;
            }
            return filename;
        }

        
        /// <summary>
        /// evento do buttao 3 que permite escolha do csv
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click_1(object sender, EventArgs e)
        {
            string filename = this.fileDialog();
            this.teachers = new List<Teacher>();
            try
            {
                var csv = new CsvReader(new StreamReader(filename), true);
                label1.Text = "File: " + filename;
                while(csv.ReadNextRecord())
                {
                    this.teachers.Add(new Teacher(csv[0],csv[1],csv[2],csv[3],csv[4],csv[5],csv[6],csv[7],csv[8]));
                }
                
                dataGridView1.DataSource = this.teachers;
                Dump.Enabled = true;
            }
            catch {
            
            }     
        }
        /// <summary>
        /// evento do buttao dump que permite iniciar a descarga dos registos para a base de dados
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Dump_Click(object sender, EventArgs e)
        {
            SQLHandler sql = new SQLHandler(DBName.Text);
            sql.createTable(TableName.Text);
            sql.insertData(TableName.Text, teachers);
            MessageBox.Show("Processe completed");            
        }

        /// <summary>
        /// devolve o numero do row conforme a estatistica selecionada
        /// </summary>
        /// <returns></returns>
        private int getSelectStat()
        {
            if (Catgory.Checked)
                return 4;
            else
                return 2;
        }

        /// <summary>
        /// evento do buttao de estatisticas
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Statistic_Click(object sender, EventArgs e)
        {
            StatistcGui form2 = new StatistcGui(DBName.Text, TableName.Text, this.getSelectStat());
            form2.Show();
            try
            {
                
            }
            catch
            {
                MessageBox.Show("Insert data base name and table name correct!");
            }

            
        }

        
    }   

   
}
